/* @flow */

import config from '../config'
import { ASSET_TYPES } from 'shared/constants'
import {
  warn,
  // isPlainObject,
  camelize,
  capitalize,
} from '../util/index'

export function initAssetRegisters(Vue: GlobalAPI) {
  /**
   * Create asset registration methods.
   */
  ASSET_TYPES.forEach(type => {
    Vue[type] = function(
      id: string,
      definition: Function | Object,
    ): Function | Object | void {
      if (!definition) {
        return this.options[type + 's'][id]
      } else {
        /* istanbul ignore if */
        if (process.env.NODE_ENV !== 'production') {
          if (type === 'component' && config.isReservedTag(id)) {
            warn(
              'Do not use built-in or reserved HTML elements as component ' +
                'id: ' +
                id,
            )
          }
        }
        // if (type === 'component' && isPlainObject(definition)) {
        //   definition.name = definition.name || id
        //   definition = this.options._base.extend(definition)
        // }
        if (type === 'directive' && typeof definition === 'function') {
          definition = { bind: definition, update: definition }
        }
        /**
         * react-vue change
         */
        if (type === 'component' && typeof definition === 'function') {
          id = capitalize(camelize(id))
          if (definition.name) {
            const _id = capitalize(camelize(definition.name))
            if (_id !== id) {
              this.options[type + 's'][_id] = definition
            }
          }
        }

        this.options[type + 's'][id] = definition
        return definition
      }
    }
  })
}
